package com.elock.intf.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.elock.base.core.Pageable;
import com.elock.base.core.dao.impl.DaoCenter;
import com.elock.base.core.util.ConvertUtil;
import com.elock.entity.SaleOrg;

/**
 * Dao - 组织
 * 
 * @author 仁德
 */
@Repository("intfSaleOrgDao")
public class IntfSaleOrgDao extends DaoCenter {

	public List<Map<String, Object>> findList(Pageable pageable, Object[] args) {
		List<Object> list = new ArrayList<Object>();

		String companyCode = (String) args[0];
		String saleOrgName = (String) args[1];
		String saleOrgSn = (String) args[2];
		Boolean isEnabled = (Boolean) args[3];

		StringBuilder sql = new StringBuilder();
//		if (ConvertUtil.isEmpty(saleOrgName)) {
//			sql.append("select s.*,sd.value, 0 as is_search from sale_org s");
//			sql.append(
//					" left join system_dict sd on s.type = sd.id and sd.parent is not null");
//			sql.append(" left join company_info c on c.id=s.company_info_id");
//			sql.append(" where s.parent is null");
//		}
//		else {
			sql.append("SELECT s.*,sd.value,1 as is_search from sale_org s");
			sql.append(
					" left join system_dict sd on s.type = sd.id and sd.parent is not null");
			sql.append(" left join company_info c on c.id=s.company_info_id");
			sql.append(" WHERE 1=1");
//		}
		if (companyCode != null) {
			sql.append(" and c.company_code=?");
			list.add(companyCode);
		}
		if (!ConvertUtil.isEmpty(saleOrgName)) {
			sql.append(" and s.name like ?");
			list.add("%" + saleOrgName + "%");
		}
		if (!ConvertUtil.isEmpty(saleOrgSn)) {
			sql.append(" and s.sn like ?");
			list.add("%" + saleOrgSn + "%");
		}
		if (isEnabled != null) {
			if (isEnabled)
				sql.append(" and s.is_enabled = 1");
			else
				sql.append(" and s.is_enabled = 0");
		}

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}
		List<Map<String, Object>> saleOrgs = getNativeDao().findListMap(sql.toString(),
				objs,
				0);
		String cSql = "";
		for (Map<String, Object> saleOrg : saleOrgs) {
			String tree_path_name = "";
			String tree_path = saleOrg.get("tree_path").toString();
			if (tree_path.length() > 1) {
				tree_path = tree_path.substring(1);
				tree_path = tree_path.substring(0, tree_path.length() - 1);
				cSql = "select group_concat(name separator ' / ') tree_path_name from sale_org where id in ("
						+ "SELECT id from sale_org so WHERE so.id in("
						+ tree_path
						+ ") ORDER BY so.grade asc"
						+ ")";
				tree_path_name = getNativeDao().findString(cSql, null);
			}
			saleOrg.put("tree_path_name", tree_path_name);
		}

		return saleOrgs;

	}

	public SaleOrg findBySaleOrgSn(String sn) {

		if (ConvertUtil.isEmpty(sn)) {
			return null;
		}
		String sql = "select * from sale_org where sn = ?";
		SaleOrg saleOrg = getNativeDao().findSingleManaged(sql,
				new Object[] { sn },
				SaleOrg.class);
		return saleOrg;
	}
}